﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using System.Text;
using Microsoft.Phone.Shell;
using System.Diagnostics;

namespace unifiqAnthony
{
    public partial class ViewGroup : PhoneApplicationPage
    {
        WebClient client;
        private List<DisplayGroup> displayGrpList = new List<DisplayGroup>();

        public ViewGroup()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;

        }

        private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            string url = "http://unifiq.5kportal.com/retrieve_bing_group.php";
            Uri uri = new Uri(url);
            client = new WebClient();
            client.AllowReadStreamBuffering = true;

            client.DownloadStringCompleted += client_DownloadStringCompleted;
            client.DownloadProgressChanged += client_DownloadProgressChanged;

            client.DownloadStringAsync(uri);
        }

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Cancelled == false & e.Error == null)
            {
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
                ObservableCollection<Group> list = new ObservableCollection<Group>();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ObservableCollection<Group>));
                list = (ObservableCollection<Group>)serializer.ReadObject(ms);

                foreach (Group grp in list)
                {
                    Uri uri = new Uri(grp.imagePath, UriKind.Absolute);
                    BitmapImage bm = new BitmapImage(uri);
                    Debug.WriteLine(grp.groupID.ToString());
                    displayGrpList.Add(new DisplayGroup(grp.groupID, grp.name, grp.description, bm));
                }

                group_lb.ItemsSource = displayGrpList;

            }

        }

        private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {

        }

        private void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                DisplayGroup selectedGroup = (DisplayGroup)e.AddedItems[0];
                ((ListBox)sender).SelectedIndex = -1;
                Debug.WriteLine(selectedGroup.groupID.ToString());
                NavigationService.Navigate(new Uri("/Planner.xaml?groupID=" + selectedGroup.groupID.ToString() + "&groupName=" + selectedGroup.name, UriKind.Relative));      
            }

        }

    }

    [DataContract]
    public class Group
    {
        [DataMember]
        public int groupID { get; set; }

        [DataMember]
        public string name { get; set; }

        [DataMember]
        public string description { get; set; }

        [DataMember]
        public string imagePath { get; set; }

        public Group(int groupID, string name, string description, string imagePath)
        {
            this.groupID = groupID;
            this.name = name;
            this.description = description;
            this.imagePath = imagePath;
        }
    }

    public class DisplayGroup
    {
        public int groupID { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public BitmapImage img { get; set; }

        public DisplayGroup(int groupID, string name, string description, BitmapImage img)
        {
            this.groupID = groupID;
            this.name = name;
            this.description = description;
            this.img = img;
        }
    }
}